#include "personalwidget.h"
#include "ui_personalwidget.h"
#include<QFileDialog>
personalwidget::personalwidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::personalwidget)
{
    ui->setupUi(this);
    init();
    ui->ft->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->ft->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->ft->horizontalHeader()->setDefaultSectionSize(217); // 设置列宽
    ui->ft->verticalHeader()->setDefaultSectionSize(164); // 设置行高
    ui->ct->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->ct->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->ct->resizeColumnsToContents();
    ui->ct->resizeRowsToContents();
    ui->ct->horizontalHeader()->setDefaultSectionSize(162); // 设置列宽
    ui->ct->verticalHeader()->setDefaultSectionSize(180); // 设置行高
    connectsignals();
}

personalwidget::~personalwidget()
{
    delete ui;
}

void personalwidget::setcurindex(int index)
{
    ui->stackedWidget->setCurrentIndex(index);
}



void personalwidget::initcollects(QHash<QString, QVector<onevideoinfo*> > collects,QHash<QString,firstcollectvideo*> collectfirstvideo)
{
    //初始化收藏数据
    this->collects=collects;
    this->collectfirstvideo=collectfirstvideo;
    int listwidgetindex=0;
    //初始化收藏夹列表ui界面
    for(auto key:collectfirstvideo.keys())
    {
        collectitem* ci=new collectitem;
        ci->init(key,collects[key].size(),collectfirstvideo[key]->image);
        QListWidgetItem* li=new QListWidgetItem(ui->collectlist);
        li->setSizeHint(QSize(ui->collectlist->width(),50));
        collectlistitems[key]=ci;     //添加一个收藏项到收藏项列表中
        ui->collectlist->setItemWidget(li,ci);  //加入到收藏夹显示列表
        collectsrownum[listwidgetindex++]=key;  //通过listwidget行号检索某个收藏夹
    }
    if(ui->collectlist->count()>0)
    ui->collectlist->setCurrentRow(0);
}

void personalwidget::connectsignals()
{
    connect(creco,&createcollect::ablecreateonecollect,this,[=](QString image,QString name){
        collectitem* ci=new collectitem(ui->collectlist);
        collectlistitems[name]=ci;
        ci->init(name,0,image);
        QListWidgetItem* li=new QListWidgetItem(ui->collectlist);
        li->setSizeHint(QSize(ui->collectlist->width(),50));
        ui->collectlist->setItemWidget(li,ci);  //加入到收藏夹显示列表
        //创建firstvideo并更新到collectfirstvideo中去
        firstcollectvideo* fi=new firstcollectvideo;
        fi->init(image,name);
        collectfirstvideo[name]=fi;
        //更新collectrownum
        int pos=collectsrownum.size();
        collectsrownum[pos]=name;
        //同时将加添加数据更新到数据库中去
        db->addonecollect(pri,name,image);
        //发送信号主界面接收，随后转发到detailedvideoitem中更新需要操作的收藏夹
        emit addonecollectitem(ci);
    });
    connect(amv,&addmyvideo::addonemyvideo,this,[=](int videoid,QString videoaddress,QString videoimage,QString videolenth){
        //将添加的视频更新到ui界面中
        QString author=pri->name;
        QString sign=QString("a video of %1").arg(author);
        onevideoinfo* ones=new onevideoinfo;
        ones->videoaddress=videoaddress;
        ones->videoid=videoid;
        ones->videolength=videolenth;
        ones->image=videoimage;
        ones->sign=sign;
        ones->author=author;
        ones->displayvideolength=false;
        ones->authorimage=pri->image;
        QDate currentDate = QDate::currentDate();
        int year = currentDate.year();
        int month = currentDate.month();
        int day = currentDate.day();
        ones->issuetime=QString("%1-%2-%3").arg(year).arg(month).arg(day);
        ones->initUi();
        ui->myvideo->setCellWidget(myvideonextrow,myvideonextcol,ones);  //width:210  height:201
        //更新到数据库中
        db->addonemyvideo(ones);
    });
}

void personalwidget::init()
{
    creco=new createcollect;
    creco->setGeometry(150,40,250,227);
    creco->hide();
    amv=new addmyvideo;
    amv->setGeometry(300,300,400,400);
    amv->hide();
}




void personalwidget::initprivateinfo(privateinfo *pri)
{
    this->pri=pri;
    ui->name->setText(pri->name);
    ui->sign->setText(pri->sign);
    ui->image->setPixmap(QPixmap(pri->image));
}

void personalwidget::initdatabase(databasemanage *db)
{
    this->db=db;
}

void personalwidget::initmyvideo(QVector<onevideoinfo *> onevideos)
{
    for(int i=0;i<onevideos.size();i++)
    {
        //存到数组中保存
        myvideos[{myvideonextrow,myvideonextcol}]=onevideos[i];
        //更新ui
        ui->myvideo->setCellWidget(myvideonextrow,myvideonextcol,onevideos[i]);
        myvideonextcol++;
        if(myvideonextcol==ui->myvideo->columnCount())
        {
            myvideonextcol=0;
            myvideonextrow++;
        }
        if(myvideonextrow==ui->myvideo->rowCount())
        {
            qDebug()<<"我的视频已经存放满了，无法继续加载";
            return;
        }
    }
}

void personalwidget::addonevideotocollect(QString collectname, onevideoinfo *oneinfo)
{
    collects[collectname].push_back(oneinfo);
    //更新收藏夹数量
    if(collectlistitems.find(collectname)==collectlistitems.end())
    {
        qDebug()<<"154";
    }
    collectlistitems[collectname]->collectnum++;
    collectlistitems[collectname]->renewui();
    renewcollectcontent(collects[collectname]);
}

void personalwidget::renewcollectcontent(QVector<onevideoinfo *> ones)
{
    mycollectvideos.clear();
    int row=0;
    int col=0;
    for(int i=0;i<ones.size();i++)
    {
        onevideoinfo* on=new onevideoinfo;
        //要复制一份的，因为当初设计的是只创建一个实例，其余类想使用全部通过指针访问
        on->duplicateone(ones[i]);
        on->initUi();
        mycollectvideos[{row,col}]=on;
        ui->ct->setCellWidget(row,col,on);
        col++;
        if(col==3)
        {
            row++;
            col=0;
        }
        if(row==3)
        {
            qDebug()<<"收藏夹内容已满，无法添加";
            return;
        }
    }
}

void personalwidget::on_mainwidgetbtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}


void personalwidget::on_collectbtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);

}




void personalwidget::on_createcollect_clicked()
{
    creco->show();
}


void personalwidget::on_collectlist_currentRowChanged(int currentRow)
{
    if(collectsrownum.find(currentRow)!=collectsrownum.end())
    {
        QString collectname=collectsrownum[currentRow];
        firstcollectvideo* fi=collectfirstvideo[collectname];
        firstcollectvideo* du_fi=new firstcollectvideo;
        du_fi->init(fi->image,fi->name);
        ui->ft->removeCellWidget(0,0);
        ui->ft->setCellWidget(0,0,du_fi);
        //更新收藏夹中的内容和ui
        ui->ct->clear();
        QVector<onevideoinfo*> ones=collects[collectname];
        renewcollectcontent(ones);
    }
    else
        qDebug()<<"personalwidget 103 error";
}



void personalwidget::on_addvideobtn_clicked()
{
    //添加一个自己的视频--弹出视频选择对话框
    amv->show();

}


void personalwidget::on_myvideo_cellClicked(int row, int column)
{
    if(myvideos.find({row,column})==myvideos.end())
        return;
    onevideoinfo* ones=myvideos[{row,column}];
    //进入到具体视频项中
    emit enterdetailedvideo(ones);
}


void personalwidget::on_ct_cellClicked(int row, int column)
{
    if(mycollectvideos.find({row,column})==mycollectvideos.end())
        return;
    onevideoinfo* one=mycollectvideos[{row,column}];
    emit enterdetailedvideo(one);
}

